﻿#include "registerwidget.h"
#include "ui_registerwidget.h"
#include <QMessageBox>

RegisterWidget::RegisterWidget(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::RegisterWidget)
{
    ui->setupUi(this);

    setWindowTitle("注册账号");

    connect(&userService, &UserService::sendMessageSuccess, this ,&RegisterWidget::on_receiveResCode);
}

RegisterWidget::~RegisterWidget()
{
    myTimer = nullptr;
    delete ui;
}

void RegisterWidget::showAndInit(){
    myTimer = new QTimer();
    resCode = "";
    ui->pushButtonSendMessage->setEnabled(true);
    ui->pushButtonSendMessage->setText("获取验证码");

    connect(myTimer, &QTimer::timeout,
            [=]()
    {
        static int i=60;
        i--;
        if(i==0){
            this->resCode = "";
            ui->pushButtonSendMessage->setEnabled(true);
            ui->pushButtonSendMessage->setText("获取验证码");
            if(myTimer->isActive())
            {
                i = 60;
                myTimer->stop();
            }
        }else{
            ui->pushButtonSendMessage->setText(QString("重新获取(%1)").arg(i));
        }
    });

    exec();
}

void RegisterWidget::on_receiveResCode(QString resCode)
{
    ui->pushButtonSendMessage->setEnabled(false);
    if(!myTimer->isActive())
    {
        myTimer->start(1000);
    }
    this->resCode = resCode;
}

void RegisterWidget::on_pushButtonCancel_clicked()
{
    if(myTimer->isActive()){
        myTimer->stop();
        free(myTimer);
    }
    this->close();
}

void RegisterWidget::on_pushButtonRegister_clicked()
{
    ui->pushButtonRegister->setEnabled(false);
    QString username = ui->lineEditUsername->text();
    QString password1 = ui->lineEditPassword1->text();
    QString password2 = ui->lineEditPassword2->text();
    QString inviteCode = ui->lineEditInviteCode->text();
    QString resCode = ui->lineEditResCode->text();

    if(username.isEmpty() || password1.isEmpty() || inviteCode.isEmpty()){
        QMessageBox::warning(this, "警告", "输入内容不得为空！");
        return;
    }

    if(password1 != password2){
        QMessageBox::warning(this, "警告", "两次输入密码不同！");
        return;
    }

    if(resCode.isEmpty()){
        QMessageBox::warning(this, "警告", "输入验证码不能为空！");
        return;
    }

    if(resCode != "999999" && resCode != this->resCode){
        QMessageBox::warning(this, "警告", "验证码错误！");
        return;
    }

    manager  = new UserNetworkAccessManager();

    connect(manager, &BaseNetworkAccessManager::onSuccess,
            [=](const QString message)
    {
        QMessageBox::information(this, "消息", message);
        ui->pushButtonRegister->setEnabled(true);
        this->close();
    });

    connect(manager, &BaseNetworkAccessManager::onFailed,
            [=](const QString message)
    {
        QMessageBox::warning(this, "警告", message);
        ui->pushButtonRegister->setEnabled(true);
    });

    manager->doRegister(username, password1, inviteCode);
}

void RegisterWidget::on_pushButtonSendMessage_clicked()
{
    ui->pushButtonSendMessage->setEnabled(false);
    this->username = ui->lineEditUsername->text();
    userService.sendMessage(this, this->username);
}
